/**
 * Created by dale on 2017/8/4.
 */
var currentDepId;
var  depList;
var iDepId;
$(document).ready(function () {
    var form=new xhForm($('#xh_form'));
    depList= $('#depList');
    $('.xh-datagrid-toolbar').parseToolbar();

    //设置禁用
    $('#bt_list_del').setButtonDisable('eu');
    $('#bt_list_manage').setButtonDisable('eu');

    form.parse();
    form.bindRequired($('#bt_form_add,#bt_form_edit'));//绑定事件


    depList.treegrid({
        onSelect:function (row) {
            currentDepId=row['iDepId'];
            depList.treegrid('expand',currentDepId);
            $('#bt_list_del').setButtonEnable('eu');
            $('#bt_list_manage').setButtonEnable('eu');
        },
        onBeforeSelect:function (row) {
            if(currentDepId==undefined){
                return true;
            }else {
                if(currentDepId==row['iDepId']){
                    depList.treegrid('unselect',currentDepId);
                    depList.treegrid('collapse',currentDepId);
                    currentDepId=null;
                    $('#bt_list_del').setButtonDisable('eu');
                    $('#bt_list_manage').setButtonDisable('eu');
                    return false;
                }
            }
        },
        onBeforeCheck:function (row) {
            if(currentDepId==undefined){
                return true;
            }else {
                if(currentDepId==row['iDepId']){
                    depList.treegrid('uncheckRow',currentDepId);
                    currentDepId=null;
                    $('#bt_list_del').setButtonDisable('eu');
                    $('#bt_list_manage').setButtonDisable('eu');
                    return false;
                }
            }
        }

    });




    $('#bt_list_add').click(function () {

        var obj=depList.treegrid('getSelections')[0];

        if(obj!=undefined){
            window.top.xhTop.showModal({
                title:'新增部门',
                content: '<iframe src="'+moduleUrl+'department/depForm?iParentId='+obj['iDepId']+'"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
                button:'提交',
                backClose: false
            });
        }else {
            window.top.xhTop.showModal({
                title:'新增部门',
                content: '<iframe src="'+moduleUrl+'department/depForm"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
                button:'提交',
                backClose: false
            });
        }

    });

    $('#bt_list_del').click(function () {   //删除
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id=depList.treegrid('getSelections')[0]['iDepId'];
        myWindow.initPopUp({title: '确认删除', text: '你确定要删除这个部门吗?该部门下的子部门也将删除'},function () {
            $.ajax({
                url:moduleUrl+'department/delDep',
                data:{id:id},
                type:'POST',
                success:function (data) {
                    if(data == 'fail'){
                        window.top.xhTop.showMessage({title: '操作失败！', type: 'error'});
                        return false;
                    }
                    location.reload();
                },
                error:function (data) {
                    console.log(data);
                }
            });
        })
    });

    $('#bt_list_manage').click(function () {  //编辑
        if(xhExtra.buttonDisabled($(this))){
            return false;
        }
        var id=depList.treegrid('getSelections')[0]['iDepId'];

        window.top.xhTop.showModal({
            title:'编辑部门',
            content: '<iframe src="'+moduleUrl+'department/depForm?iDepId='+id+'"  style="width: 100%;;border-style: none;padding: 0px;height: 220px"  scrolling="yes"   ></iframe>',
            button:'提交',
            backClose: false,
        });
    });




    // //树形点击
    // function bindClick() {
    //     $('.tree-view-menu-list .openable a').click(function()	{
    //         var parentElm = $(this).parent();
    //         parentElm.toggleClass('open');
    //         parentElm.children('.subtree').slideToggle(200);
    //         return false;
    //     });
    //
    //     //导航点击事件
    //     $('.tree-view-menu-list a').click(function(){
    //
    //         if($(this).attr('nodeId')!=iDepId&&!isChildren($(this).attr('nodeId'),iDepId)){ //不是同一个,或者不是自己的子节点
    //
    //             $('#inputPareName').val($(this).text());
    //             $('#xh_form [name="iParentId"]').val($(this).attr('nodeId'));
    //         }
    //     });
    //
    //    
    // }

    
    
    //------------------------------------------表格部分-------------------------------------------------------------
    // $('#bt_form_add').click(function () {   //添加
    //     $.ajax({
    //         url:moduleUrl+'department/saveDepartment',
    //         data:$('#xh_form').serialize(),
    //         type:'POST',
    //         success:function (data) {
    //             if(data == 'fail'){
    //                 window.top.xhTop.showMessage({title: '操作失败！', type: 'error'});
    //                 return false;
    //             }
    //             form.reset();
    //             location.reload();
    //         },
    //         error:function (data) {
    //             console.log(data);
    //         }
    //
    //     })
    // });
    //
    // $('#bt_form_edit').click(function () {  //编辑
    //
    //     $.ajax({
    //         url:moduleUrl+'department/saveDepartment',
    //         data:$('#xh_form').serialize(),
    //         type:'POST',
    //         success:function (data) {
    //             if(data == 'fail'){
    //                 window.top.xhTop.showMessage({title: '操作失败！', type: 'error'});
    //                 return false;
    //             }
    //             form.reset();
    //             location.reload();
    //
    //         },
    //         error:function (data) {
    //             console.log(data);
    //         }
    //     });
    // });
    //
    //
    //
    //
    // $('#bt_form_reset').click(function () {
    //     form.reset();
    // });
    //
    // $('#bt_form_return').click(function () {
    //     $('#depTree').show();
    //     $('#xh_form').parent().parent().hide();
    //     form.reset();
    // });

});

